package com.eastmoney.emis.edda.controller.inner;

import com.eastmoney.emis.edda.application.vo.AuthApplyLimitDailyVo;
import com.eastmoney.emis.edda.domain.repository.EddaApplyTimesLimitRepository;
import com.eastmoney.emis.edda.domain.repository.HsbcEddaRepository;
import com.eastmoney.emis.edda.domain.service.edda.impl.BindCheckDailyCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.controller.inner
 * @author: 汪程程
 * @date: 2023/2/15 10:45 AM
 * @version: 1.0
 */
@RequestMapping("/innerApi/hsbc")
@RestController
public class BindCheckDailyController {
    @Autowired
    private BindCheckDailyCache bindCheckDailyCache;
    @Autowired
    private HsbcEddaRepository hsbcEddaRepository;
    @Autowired
    private EddaApplyTimesLimitRepository timesLimitRepository;

    @PutMapping("/restrictions/lift/{clientId}")
    public void liftBindCheckLimit(@PathVariable(name = "clientId") String clientId) {
        bindCheckDailyCache.add(clientId);
    }

    @GetMapping("/restrictions/{clientId}")
    public AuthApplyLimitDailyVo bindCheckLimit(@PathVariable(name = "clientId") String clientId) {
        LocalDateTime dateTime = bindCheckDailyCache.getObject(clientId);
        if(dateTime == null) {
            dateTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        }
        int limitTimesDaily = timesLimitRepository.getMaxApplyTimesDaily();
        int applyTimesDaily = hsbcEddaRepository.getEddaApplyTimesDailyByClientIdAndStartTime(clientId, dateTime);
        return new AuthApplyLimitDailyVo(applyTimesDaily, limitTimesDaily);
    }
}
